package com.bluetooth;

import androidx.appcompat.app.AppCompatActivity;
import com.bluetooth.ble.BluetoothLeService;
import com.itianshan.wheel.R;
import com.itianshan.wheel.WheelView;
import com.itianshan.wheel.WheelView.OnWheelViewMoveListener;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;


public class BluetoothWheelActivity extends AppCompatActivity {
    public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
    public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";
    private int speed;
    private int direction;

    private WheelView wheelView_01,wheelView_02;
    private TextView textView_01,textView_02;
    private String mDeviceName;
    private String mDeviceAddress;
    int tx_count = 0;
    private TextView mConnectionState;
    private TextView send_count;
    boolean send_hex = true;//HEX格式发送数据  透传
    private BluetoothIOClass bluetoothIO;  //蓝牙操作器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        		//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				//WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.wheel_main);
        initView();
        bluetoothCommander(); //蓝牙操作
    }
    private void initView() {
        mConnectionState = (TextView) findViewById(R.id.connection_state);
        send_count=(TextView) findViewById(R.id.send_count);
        wheelView_01 = (WheelView) findViewById(R.id.wheelView_01);
        textView_01 = (TextView) findViewById(R.id.textView_01);
        wheelView_02 = (WheelView) findViewById(R.id.wheelView_02);
        textView_02 = (TextView) findViewById(R.id.textView_02);

        wheelView_01.setOnWheelViewMoveListener(
                new OnWheelViewMoveListener() {
                    @Override
                    public void onValueChanged(int angle, int distance) {
                        textView_01.setText("角度：" + angle + "\n" +	"距离：" + distance);
                        if(angle<-115 && -180<angle){
                            angle = -(180+angle);
                        }
                        if (115<angle && angle<180){
                            angle = 180-angle;
                        }
                        if(!(angle>-65 && angle <65)){
                            angle = direction;
                        }
                        String dir = angle==0?"":(angle>0?"右":"左");
                        textView_01.setText("方向：" +dir+ Math.abs(angle) + "\n");

                        if( bluetoothIO.mConnected && bluetoothIO.connect_status_bit && direction!=angle)
                        {
                                tx_count+=bluetoothIO.mBluetoothLeService.txxx( "{\"dir\":"+String.valueOf(angle)+"}\r\n",send_hex );//发送字符串数据
                                send_count.setText("发送数据："+tx_count);
                                //mBluetoothLeService.txxx( tx_string,false );//发送HEX数据
                        }else{
                            //Toast.makeText(this, "Deleted Successfully!", Toast.LENGTH_LONG).show();
                            //Toast toast = Toast.makeText(MainActivity.this, "设备没有连接！", Toast.LENGTH_SHORT);
                            //toast.show();
                        }
                        direction = angle;
                    }
                }, 100L);
        wheelView_02.setOnWheelViewMoveListener(
                new OnWheelViewMoveListener() {
                    @Override
                    public void onValueChanged(int angle, int distance) {
                        //textView_02.setText("角度：" + angle + "\n" +	"距离：" + distance);
                        if(!(angle>-95 && angle <95)){
                            distance = -distance/10;
                        }
                        textView_02.setText("速度：" + distance+"%"+(distance==0?"":(distance>0?"前进":"倒车")));
                        if(bluetoothIO.mConnected && bluetoothIO.connect_status_bit && speed!=distance)
                        {
                                tx_count+=bluetoothIO.mBluetoothLeService.txxx("{\"spe\":"+String.valueOf(distance)+"}\r\n",send_hex );//发送字符串数据
                                send_count.setText("发送数据："+tx_count);
                                //mBluetoothLeService.txxx( tx_string,false );//发送HEX数据
                        }else{
                            //Toast.makeText(this, "Deleted Successfully!", Toast.LENGTH_LONG).show();
                            //Toast toast = Toast.makeText(MainActivity.this, "设备没有连接！", Toast.LENGTH_SHORT);
                            //toast.show();
                        }
                        speed = distance;
                    }
                }, 100L);
    }
    /*
     * 蓝牙控制小车
     */
    private void bluetoothCommander(){
        final Intent intent = getIntent();
        mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
        mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
        ((TextView) findViewById(R.id.device_address)).setText(mDeviceAddress);
        mConnectionState = (TextView) findViewById(R.id.connection_state);

        bluetoothIO = new BluetoothIOClass(mDeviceAddress,mDeviceName,false);
        bluetoothIO.mConnectionState=mConnectionState;
        registerReceiver(bluetoothIO.mGattUpdateReceiver, bluetoothIO.makeGattUpdateIntentFilter());
        Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
        bindService(gattServiceIntent, bluetoothIO.mServiceConnection, BIND_AUTO_CREATE);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(bluetoothIO.mServiceConnection);
        if (bluetoothIO.mBluetoothLeService != null) {
            bluetoothIO.mBluetoothLeService.disconnect();
        }
        unregisterReceiver(bluetoothIO.mGattUpdateReceiver);
        bluetoothIO.mBluetoothLeService = null;
    }
}